#include "stdafx.h"

#include "LayoutPanel.h"
#include "AllLookFeel.h"
#include "LayoutEditor.h"
#include "MainStatusBar.h"

LayoutPanel::LayoutPanel()
{
	mWidgetCreateDlg = new WidgetCreateDlg;
	mWidgetCreateDlg->eventEndDialog += OnWidgetCreate(this, &LayoutPanel::OnWidgetCreate_);

	mLayout = new MGUI_Layout(AllLookFeel::Instance()->GetPanel());

	int y_base = 0;
	mListBox = new MGUI_ListBox(AllLookFeel::Instance()->GetListBox(), mLayout);
	mListBox->SetAlign(MGUI_Align::HStretch);
	mListBox->SetRect(0, y_base, 0, 24 + 256);
	y_base += 24 + 256 + 5;

	MGUI_Button * bnCreate = new MGUI_Button(AllLookFeel::Instance()->GetButton(), mLayout);
	bnCreate->SetAlign(MGUI_Align::HStretch);
	bnCreate->SetRect(MGUI_Rect(true, 0, y_base, 0, 30));
	bnCreate->SetCaption(L"Create");
	y_base += 30 + 5;

	MGUI_Button * bnDelete = new MGUI_Button(AllLookFeel::Instance()->GetButton(), mLayout);
	bnDelete->SetAlign(MGUI_Align::HStretch);
	bnDelete->SetRect(MGUI_Rect(true, 0, y_base, 0, 30));
	bnDelete->SetCaption(L"Delete");
	y_base += 30 + 5;

	bnCreate->eventMouseClick += OnCreate(this, &LayoutPanel::OnCreate_);
	bnDelete->eventMouseClick += OnDelete(this, &LayoutPanel::OnDelete_);

	mListBox->eventSelectChanged += OnWidgetSelChanged(this, &LayoutPanel::OnWidgetSelChanged_);
	
	LayoutEditor::Instance()->eventNameChanged += OnNameChanged(this, &LayoutPanel::OnNameChanged_);
	LayoutEditor::Instance()->eventOpenLayout += OnOpenLayout(this, &LayoutPanel::OnOpenLayout_);
}

LayoutPanel::~LayoutPanel()
{
	delete mLayout;

	delete mWidgetCreateDlg;
}

void LayoutPanel::Layout()
{
	MGUI_Rect rect;

	rect.x0 = 0;
	rect.y0 = 32;
	rect.x1 = 256;
	rect.y1 = MGUI_Engine::Instance()->GetRect().y1 - 32;

	mLayout->SetRect(rect);
}

void LayoutPanel::OnCreate_()
{
	mWidgetCreateDlg->DoModal();
}

void LayoutPanel::OnDelete_()
{
	int index = mListBox->GetSelectIndex();

	if (index == -1)
		return ;

	MGUI_Widget * widget = (MGUI_Widget *)mListBox->GetUserData(index);

	if (widget == LayoutEditor::Instance()->GetEditLayout())
	{
		LayoutEditor::Instance()->_distroyLayout();
		mListBox->Clear();
	}
	else
	{
		LayoutEditor::Instance()->notifyEditWidgetChanged(NULL);
		delete widget;
		mListBox->Remove(index);
	}
}

void LayoutPanel::OnWidgetCreate_(bool _ok)
{
	if (_ok)
	{
		int type = mWidgetCreateDlg->GetSelectType();
		MGUI_LookFeel * lookfeel = mWidgetCreateDlg->GetSelectLookFeel();
		bool first = false;

		MGUI_Layout * parent = LayoutEditor::Instance()->GetEditLayout();

		if (parent == NULL)
		{
			LayoutEditor::Instance()->_createLayout();
			parent = LayoutEditor::Instance()->GetEditLayout();

			mListBox->Append(parent->GetName().c_wstr(), parent);

			first = true;
		}

		if (type != -1 && (lookfeel != NULL || type == MGUI_WidgetType::Widget || type == MGUI_WidgetType::Label))
		{
			MGUI_Widget * _widget = MGUI_Helper::Instance()->CreateWidget(type, lookfeel, parent);

			_widget->SetName("Widget");

			mListBox->Append(_widget->GetName().c_wstr(), _widget);

			if (first)
				parent->SetRect(_widget->GetRect());
			else
			{
				MGUI_Rect rect = parent->GetRect();

				if (rect.x0 < 0 && rect.y0 < 0)
					_widget->Move(-rect.x0, -rect.y0);
				else if (rect.x0 < 0 && rect.y0 >= 0)
					_widget->Move(-rect.x0, 0);
				else if (rect.x0 >= 0 && rect.y0 < 0)
					_widget->Move(0, -rect.y0);
			}

			LayoutEditor::Instance()->notifyEditWidgetChanged(_widget);

			mListBox->SetSelectIndex(mListBox->GetCount() - 1);
		}
	}

}

void LayoutPanel::OnWidgetSelChanged_(int _index)
{
	if (_index == -1)
	{
		LayoutEditor::Instance()->notifyEditWidgetChanged(NULL);
	}
	else
	{
		MGUI_Widget * widget = (MGUI_Widget *)mListBox->GetUserData(_index);

		LayoutEditor::Instance()->notifyEditWidgetChanged(widget);
	}
}

void LayoutPanel::OnNameChanged_(TString128 _name)
{
	for (int i = 0; i < mListBox->GetCount(); ++i)
	{
		MGUI_Widget * widget = (MGUI_Widget *)mListBox->GetUserData(i);
		
		if (widget == LayoutEditor::Instance()->GetEditWidget())
		{
			mListBox->GetItem(i)->SetText(widget->GetName().c_wstr());
		}
	}
}

void LayoutPanel::OnOpenLayout_(MGUI_Layout * layout)
{
	mListBox->Clear();

	mListBox->Append(layout->GetName().c_wstr(), layout);

	for (int i = 0; i < layout->GetChildCount(); ++i)
	{
		MGUI_Widget * child = layout->GetChild(i);
		mListBox->Append(child->GetName().c_wstr(), child);
	}
}

bool LayoutPanel::SaveLayout(const TString128 & filename)
{
	if (mListBox->GetCount() == 0)
		return false;

	std::ofstream file;
	file.open(filename.c_str());

	if (!file.is_open())
		return false;

	xml_doc doc;

	xml_node * node = doc.allocate_node_ex(rapidxml::node_element, "Layout");

	MGUI_Widget * widget = (MGUI_Widget *)mListBox->GetUserData(0);

	widget->SaveXml(node, doc);
	
	for (int i = 1; i < mListBox->GetCount(); ++i)
	{
		widget = (MGUI_Widget *)mListBox->GetUserData(i);

		int type = MGUI_Helper::Instance()->GetWidgetType(widget);
		TString128 lookfeel = widget->GetLookFeel() ? widget->GetLookFeel()->GetName() : "";

		xml_node * child = doc.allocate_node_ex(rapidxml::node_element, "Child");

		{
			xml_attri * attri = doc.allocate_attribute_ex("type", MGUI_WidgetType::GetTypeString(type).c_str());
			child->append_attribute(attri);
		}

		{
			xml_attri * attri = doc.allocate_attribute_ex("lookfeel", lookfeel.c_str());
			child->append_attribute(attri);
		}

		widget->SaveXml(child, doc);

		node->append_node(child);
	
	}

	doc.append_node(node);

	file << doc;

	file.close();

	return true;
}